package com.wh.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.vo.AjaxResult;
import com.wh.system.annotation.PreAuthorize;
import com.wh.system.dto.MenuDto;
import com.wh.system.pojo.Menu;
import com.wh.system.service.IMenuService;
import com.wh.user.pojo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;


    /**
     * 获取菜单树-无限极接口
     * @return
     */

    @GetMapping("/menuTree")
    public AjaxResult menuTree(@RequestParam  Long roleId) {
        return AjaxResult.me().setResultObj(menuService.menuTree(roleId));
    }


    /**
     * 获取所有菜单路径
     * @return 菜单路径<菜单ID,菜单路径>
     */

    @PreAuthorize(sn = "menu/menuPath",name = "获取菜单路径")
    @GetMapping("/menuPath")
    public Map<Long,String> menuPath(){
        return menuService.menuPath();
    }


    /**
     * 接口：添加或修改
     * @param menu 传递的实体
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "menu/addOrUpdate",name = "添加或修改菜单")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Menu menu) {
        if ( menu.getId() != null){
            menuService.upd(menu);
        }else{
            menuService.add(menu);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     * @param id
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "menu/del",name = "根据i的删除菜单")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        menuService.del(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "menu/patchDel",name = "批量删除菜单")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        menuService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     * @param id
     */

    @PreAuthorize(sn = "menu/findOne",name = "根据id查找菜单")
    @GetMapping("/{id}")
    public Menu findOne(@PathVariable("id") Long id) {
        return menuService.findById(id);
    }


    /**
     * 接口：查询所有
     * @return
     */

    @PreAuthorize(sn = "menu/findAll",name = "获取全部菜单")
    @GetMapping
    public List<Menu> findAll() {
        return menuService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     * @param menuDto 查询对象
     * @return IPage<Menu> 分页对象
     */

    @PreAuthorize(sn = "menu/findByPage",name = "分页查找菜单")
    @PostMapping
    public IPage<Menu> findByPage(@RequestBody MenuDto menuDto) {
        return menuService.findByPage(menuDto);
    }



    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        LoginInfo loginInfo = new LoginInfo();
        Long id = loginInfo.getId();
        System.out.println(id+"当前登陆人");
        List<Menu> menus = menuService.selectMenuList(id);
        Map<String,Object> hashMap=new HashMap<>();
        hashMap.put("checkedKeys",menuService.selectMenuListByRoleId(roleId));
        hashMap.put("menus",menuService.buildMenuTreeSelect(menus));
        return AjaxResult.me().setSuccess(true).setResultObj(hashMap);
    }

}



